<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" @click="router.push('/geofenceMap')" style="float:right;margin-top:13px;">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称" >
        <el-input v-model="geoSearchForm.name" @keydown.enter="loadGeo" placeholder="请输入" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item  label="围栏状态" style="width:290px;">
        <el-select @change="loadGeo" v-model="geoSearchForm.status" placeholder="请选择">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true"
                       width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" @change="changeGeoStatus(scope.row.id,scope.row.status)" v-model="scope.row.status"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="manageVehicle(scope.row.id)" link>管理车辆</el-button>
          <el-button type="primary" @click="deleteGeo(scope.row.id)" link>删除</el-button>
          <el-button type="primary" @click="router.push('/geofenceMapShow?id='+scope.row.id)" link>查看</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog @close="currentSelectGeo = ''" title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="addBindVehicle">添加绑定车辆</el-button>
    <el-table :data="bindVehicles">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="doNotBind(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible">
    <el-table :data="notBindVehicles">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="doBind(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from 'qs';
import {ElMessage} from "element-plus";
import axios from 'axios';
import router from "@/router/index.js";
//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);
const bindVehicles = ref([]);
const notBindVehicles = ref([]);
const currentSelectGeo = ref('');


const geoSearchForm = ref({name: '', status: ''});
const geoTableData = ref([]);
const loadGeo = async () => {
  const {data: {data, code, msg}} =await axios.get(BASE_URL + "/v1/geofence/select?" + qs.stringify(geoSearchForm.value))
  if (code === 2000) {
    geoTableData.value = data;
  }else{
    ElMessage.error(msg)
  }
};
const resetSearch = () => {
  geoSearchForm.value = {};
  loadGeo();
};

onMounted(()=>{
  loadGeo();
})

const manageVehicle =async (id)=>{
  if(id) currentSelectGeo.value = id;
  bindVehicleDialogVisible.value = true;
  const {data:{data,code,msg}} = await axios(BASE_URL+"/v1/vehicle/getListByGeoId/"+currentSelectGeo.value)
  if(code === 2000){
    bindVehicles.value =data;
  }else {
    ElMessage.error(msg)
  }
}

const deleteGeo = async id =>{
  const {data:{data,code,msg}} = await axios.post(BASE_URL+"/v1/geofence/delete/"+id);
  if(code === 2000){
    ElMessage.success("删除成功!");
    loadGeo();
  }else {
    ElMessage.error("请先移除所有已绑定车辆！")
  }
}

const changeGeoStatus = async (id,status)=>{
  const {data:{data,code,msg}} = await axios.post(BASE_URL+'/v1/geofence/save',qs.stringify({id,status}));
  if(code === 2000){
    ElMessage.success("修改成功!")
  }else {
    ElMessage.error("修改失败!")
  }
}

const getBindVehicle = async ()=>{
  const {data:{data,code,msg}} = await axios.get(BASE_URL+"/v1/vehicle/getCanBindVehicle");
  if(code === 2000){
    notBindVehicles.value = data;
  }else {
    ElMessage.error(msg)
  }
};

const doNotBind=async id=>{
  const {data:{code,msg}} = await axios.post(BASE_URL+'/v1/vehicle/notBind/'+id);
  if(code === 2000){
    ElMessage.success("移除成功")
  }else {
    ElMessage.error("请先移除所有绑定车辆！")
  }
  manageVehicle();
}

const addBindVehicle = async ()=>{
  AddBindVehicleDialogVisible.value = true;
  getBindVehicle();
}

const closeAddBindVehicleDialogVisible =()=>{
  AddBindVehicleDialogVisible.value = false;
  manageVehicle();
}

const doBind = async vId =>{
  const {data:{code,msg}} = await axios.post(BASE_URL+"/v1/vehicle/bindGeo",qs.stringify({geoId:currentSelectGeo.value,vId}))
  if(code === 2000){
    ElMessage.success("绑定成功!")
  }else {
    ElMessage.error(msg)
  };
  closeAddBindVehicleDialogVisible();
}
</script>

<style scoped>
</style>